package com.xy.iot.netty.device;

import com.xy.iot.common.message.BaseMessage;
import com.xy.iot.netty.service.NameValueImpl;
import io.netty.channel.Channel;

import java.net.InetSocketAddress;

public class DeviceServerChannel extends NameValueImpl implements DeviceGroup {

    private final InetSocketAddress address;
    private final Channel channel;

    public DeviceServerChannel(InetSocketAddress address, Channel channel){
        this.channel = channel;
        this.address = address;
        name(this.getClass().getSimpleName() + "_" + address());
    }

    public DeviceServerChannel(Channel channel){
        this((InetSocketAddress) channel.remoteAddress(), channel);
    }

    @Override
    public int port(){
        return address.getPort();
    }

    @Override
    public String address() {
        return address.getAddress().getHostAddress();
    }

    @Override
    public void send(BaseMessage msg) {
        sendData(this.channel, msg);
    }
}
